<?php

/**
 * 武汉移动接口
 * 
 * Apps/Common/Service/CmccApiService.class.php
 * Zix @ 2015-08-31
 */

namespace Common\Service;

class CmccApiService {

  private static $instance;

  const APPID = 'hfh';
  const TOKEN = 'utqo!u1';
  const API_ROOT_URL = 'http://www.cmccwhwx.com/api/';
  
  const GET_USERINFO_URL     = 'v1.0/getuser';     //取用户信息
  const GET_VUSERINFO_URL    = 'v1.0/getvuser';     //取游客信息
  const PAY_POINTS_URL       = 'v1.0/bbexchange';  //扣帮帮豆
  const GET_WXJSSDK_DATA_URL = 'v1.0/wxjsapi';   //取wx js sdk 参数
  
  const REDIRECT_SHARE_URL = 'v1.0/vredirect'; //游客跳转链接
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new CmccApiService();
    }
    return $instance;
  }
  
  function _get_api_url( $url  ) {
    return self::API_ROOT_URL . $url . '?';
  }

  /**
   * 取分享地址
   * @param type $url
   * @return type
   */
  function get_share_url( $url ) {
    $new_url = urlencode($url . '?1=1');
    
    return $this->_get_api_url( self::REDIRECT_SHARE_URL ). 'url=' . $new_url ;
    //return self::REDIRECT_SHARE_URL . $url . '?1=1';
  }

  /**
   * 从api取用户信息
   * @param type $user_token
   */
  function get_user_info_from_api($user_token , $visitor = FALSE ) {
    if ( $user_token == 'test' && !$visitor ) {
      return array(
        'uid'        => 232824 ,
        'status'     => 1 ,
        'msg'        => 'ok' ,
        'phone'      => '15926212058' ,
        'nickname'   => 'Em!Ly_小爱' ,
        'headimgurl' => 'http://wx.qlogo.cn/mmopen/SYeWkon6C6IqS50Y2Ov4ft2fOcwQFXIP1iaia70q1ynZTwhOgCh73YYWYeZIJs0wmcUNKrDibia7usE1Cib7rJhZwtQ/96' ,
      ); 
    }
    
    if ( $user_token == 'test' && $visitor ) {
      return array(
        'vid'        => 228393 ,
        'status'     => 1 ,
        'msg'        => 'ok' ,
        'nickname'   => '豆豆' ,
        'headimgurl' => 'http://wx.qlogo.cn/mmopen/ajNVdqHZLLD6TR553F0picg4icGb0lDKpuQWKunacTNq1Pt3Q46B8r7s41hxmFAFwicMub4ovibV4qgCSzzmO358FQ/96' ,
      );  
    }

    if ( !$visitor ) {
      $data = array(
        'appid' => self::APPID ,
        'user_token' => $user_token ,
      );
      $send_data = $this->_make_sign($data);
      $send_url = $this->_get_api_url(self::GET_USERINFO_URL) . http_build_query($send_data);
    } else {
      $data = array(
        'appid' => self::APPID ,
        'vuser_token' => $user_token ,
      );
      $send_data = $this->_make_sign($data);
      
      //print_r( $send_data );
      $send_url = $this->_get_api_url(self::GET_VUSERINFO_URL) . http_build_query($send_data);
    }
    //echo $send_url ;    
    return $this->_http_get( $send_url );
  }
  
  /**
   * 签名
   * @param array $data
   * @return type
   */
  function _make_sign($data) {
    $data['token'] = self::TOKEN;
    ksort($data);
    $arr = array();
    foreach ( $data as $key => $val ) {
      $arr[] = "$key=" . $val ;
    }
    $string = implode('&', $arr);
    //echo $string . '<br>';
    $data['sign'] = sha1($string);
    unset( $data['token'] );
    return $data;
  }
  
  /**
   * curl get
   * @param type $url
   * @return type
   */
  function _http_get($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    //优化
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect: '));
    curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    $res = curl_exec($curl);
    curl_close($curl);

    //file_put_contents('./Public/tmp/cmcc.txt', date('Y-m-d H:i:s') . ' ' . $res . "\r\n" , FILE_APPEND );
    $ret = json_decode($res, TRUE);
    return $ret;
  }

  /**
   * 取微信jssdk参数
   * @param type $url
   * @return type
   */
  function get_wx_js_config( $url ) {
    $query = getenv("QUERY_STRING");
    if ( !empty( $query ) ) {
      $url .= '?' . $query ;
    }
    
    //echo $url ;
    $data = array(
      'appid'     => self::APPID ,
      'url'       => $url ,
      'timestamp' => date('YmdHis'),
    );

    $send_data = $this->_make_sign($data);
    $send_url = $this->_get_api_url(self::GET_WXJSSDK_DATA_URL) . http_build_query($send_data);
    
    return $this->_http_get( $send_url );
  }

}
